//index.js
//获取应用实例
const app = getApp()
const api = require('../../utils/api');
Page({
  data: {
    swipper_img: [
      "https://2115748.oss-cn-beijing.aliyuncs.com/his/003efd8d132135be8d88ad3b55543f06.jpeg", 
      "https://2115748.oss-cn-beijing.aliyuncs.com/his/476dcb6b540279367edebe775ad1a99a.jpeg", 
      "https://2115748.oss-cn-beijing.aliyuncs.com/his/7b82407ead14ff727997bbd9e00fce08.jpeg", 
      "https://2115748.oss-cn-beijing.aliyuncs.com/his/b2ffa61feb9cebaa77e0f631dc4fe90a.jpeg", 
      "https://2115748.oss-cn-beijing.aliyuncs.com/his/f7545d41bb5ab6a1e664c9c485f0e89e.jpeg", 
    ],
    index_btns: [{
      name: "会员", icon: "vip.png"
    }, {
      name: "结果", icon: "result.png"
    }, {
      name: "追溯", icon: "source.png"
    }, {
      name: "服务", icon: "service.png"
    }, {
      name: "挂号", icon: "reg.png"
    }, {
      name: "其他", icon: "other.png"
    }],
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  to_link(e){
    var links = ['../vip/vip', '../result/result', '../tracereg/tracereg', '../service/service', '../subcribe/subcribe', '../open/open'] 
    console.debug("index:"+e.currentTarget.dataset.index)
    wx.navigateTo({
      url: links[e.currentTarget.dataset.index],
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.debug('onload')
    this.login();
    
    console.debug('userinfo', app.globalData.userInfo)
  },
  addUser(){
    var user = app.globalData.userInfo
    console.debug('user', user)
    console.debug('openid', app.globalData.openid)
    api.myPostBody('/pet/own/login', {nickname: user.nickName, realname: user.nickName, avatar: user.avatarUrl}).then(res=>{
      console.debug('pet own login:',res)
    }).catch(err => {
      console.debug(err)
    })
  },
  login(){
    wx.login({
      complete: (res) => {
        var _this = this
        wx.request({
          url: app.globalData.url+'/session/'+res.code, 
          data: {
          },
          success (res) {
            console.log("login success data", res.data)
            // app.setData({openod: res.data.openid})
            var oid = res.data.openid
            app.globalData.openid = (oid == undefined || oid == null || oid == '')?app.globalData.openid: oid
            _this.addUser();
          }, 
          fail(err){
            console.error("login fail data",err)
          }
        })
      },
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
